/**
 * Created by no1 on 2015/12/21.
 */
'use strict';
console.log('Load O2O: ' + module.id);
let F = require('./function');
let Dao = require('./dao');
let couponDispatcher = new Dao.coupon();
let receiveLogDispatcher = new Dao.receiveLog();

function* api() {

}
exports.api = api;

/**
 * @todo 添加优惠券
 * @param Env
 */
api.add = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;
  var ret = yield couponDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 更新优惠券
 * @param Env
 */
api.update = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;
  var ret = yield couponDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 优惠券列表查询
 * @param Env
 */
api.list = function*(Env) {
  Env.I = yield F.Init(Env, [1, 2]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;
  var ret = yield couponDispatcher.list(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 优惠券详情查询
 * @param Env
 */
api.detail = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;
  var ret = yield couponDispatcher.detail(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * todo 优惠券日志添加
 * @param Env
 */
api.addLog = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;
  var ret = yield receiveLogDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * todo 优惠券日志更新
 * @param Env
 */
api.updateLog = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;
  var ret = yield receiveLogDispatcher.update(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * todo 优惠券日志列表
 * @param Env
 */
api.listLog = function*(Env) {
  Env.I = yield F.Init(Env, [1,2]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;
  var ret = yield receiveLogDispatcher.list(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * todo 优惠券日志详情
 * @param Env
 */
api.detailLog = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }

  let fields = Env.I;
  var ret = yield receiveLogDispatcher.detail(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};